﻿using BBICMS.Core;
using BBICMS.Infrastructure.Helper;
using BBICMS.Model.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;

namespace BBICMS.Model.Security
{
    [Table("Users")]
    public partial class User : Entity<Guid>
    {
        
        public User()
        {
            this.Id = IdentityGenerator.SequentialGuid();
            this.EntityName = "用户";
            this.Roles = new HashSet<Role>();
        }
        [Required]
        [Display(Name = "用户名")]

        public string UserName { get; set; }
        [Required]
        [Display(Name = "邮箱")]
        public string Email { get; set; }
     
    

        [Required]
        public string PasswordHash { get; set; }
        public string SecurityStamp { get; set; }

        [Display(Name = "姓名")]
        public string RealName { get; set; }

        [Display(Name = "公司")]
        [StringLength(150)]
        public string Company { get; set; }
        [Display(Name = "联系电话")]
        [StringLength(50)]
        public string Phone { get; set; }

        [Display(Name = "照片")]
        public string PhotoUrl { get; set;}
           
        
        [Display(Name = "激活")]
        public bool IsActive { get; set; }
        [Display(Name = "创建日期"), DisplayFormat(DataFormatString = "{0:d}")]       
        public DateTime CreateDate { get; set; }
        [Display(Name = "最后登录时间")]
        public DateTime? LastActivityDate { get; set; }

        //public Nullable<int> DepartmentId { get; set; }
        //public Nullable<int> PositionId { get; set; }

        [Display(Name = "生日"), DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime? Birthday { get; set; }
        [Display(Name = "性别")]
        public Gender Gender { get; set; }
        [Display(Name = "性别")]
        public string GenderName
        {
            get
            {
                var enumType = typeof(Gender);
                var field = enumType.GetFields()
                           .First(x => x.Name == System.Enum.GetName(enumType, Gender));
                var attribute = field.GetCustomAttribute<DisplayAttribute>();
                return attribute.Name;

            }
        }
       
        public string QQ { get; set; }
        [Display(Name = "手机")]
        public string Mobile { get; set; }

        public virtual ICollection<Role> Roles { get; set; }
        //public virtual Department Department { get; set; }
        //public virtual Position Position { get; set; }

        //[NotMapped]
        //public List<UserMenuDTO> Menus { get; set; }
      

        public bool HasMenu(string area, string controller, string action)
        {
            bool bFound = false;
            foreach (Role role in this.Roles)
            {
                bFound = (role.Menus.Where(
                          p => p.Controller == controller && p.Action == action && p.Area == area).ToList().Count > 0);
                if (bFound)
                    break;
            }
            return bFound;
        }

        public bool HasRole(string role)
        {
            return (Roles.Where(p => p.RoleName == role).ToList().Count > 0);
        }

        public bool HasRoles(string roles)
        {
            bool bFound = false;
            string[] _roles = roles.ToLower().Split(',');
            foreach (Role role in this.Roles)
            {
                try
                {
                    bFound = _roles.Contains(role.RoleName.ToLower());
                    if (bFound)
                        return bFound;
                }
                catch (Exception)
                {
                }
            }
            return bFound;
        }
    }


}